package com.iceengine.input.mouse;

/**
 * The MouseListener class must run as an own thread to listen to the mouse.
 * @author penguin
 */
import java.util.ArrayList;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.input.Mouse;

public class MouseListener implements Runnable {
    private boolean running;
    private Vector2f curPosition;
    private Vector2f lastPosition;
    private ArrayList<IMouseEvent> Events;
    private boolean isInWindow;

    public void run()
    {
        running = true;
        Events = new ArrayList<IMouseEvent>();
        lastPosition = new Vector2f(Mouse.getX(), Mouse.getY());
        while((lastPosition.x == 0) && (lastPosition.y == 0))
        {
            lastPosition = new Vector2f(Mouse.getX(), Mouse.getY());
        }
        isInWindow = Mouse.isInsideWindow();

        while(running)
        {
            if(Mouse.isInsideWindow() && isInWindow) {
                curPosition = new Vector2f(Mouse.getX(), Mouse.getY());
                while((curPosition.x == 0) && (curPosition.y == 0))
                {
                    curPosition = new Vector2f(Mouse.getX(), Mouse.getY());
                    if(!(curPosition.x == 0) && (curPosition.y == 0))
                    {
                        lastPosition = curPosition;
                    }
                }
                if (curPosition.y != lastPosition.y)
                {
                    for(int i = 0; i < Events.size(); i++)
                    {
                        Events.get(i).MouseChangedY((int)(curPosition.y - lastPosition.y));
                    }
                }

                if (curPosition.x != lastPosition.x)
                {
                    for(int i = 0; i < Events.size(); i++)
                    {
                        Events.get(i).MouseChangedX((int)(curPosition.x - lastPosition.x));
                    }
                }
                lastPosition = curPosition;
            } else if(Mouse.isInsideWindow() && !isInWindow)
            {
                lastPosition = new Vector2f(Mouse.getX(), Mouse.getY());
                isInWindow = true;
            } else if(!Mouse.isInsideWindow())
            {
                isInWindow = false;
            }
            
            try
                {
                    Thread.currentThread().sleep(20);
                }
            catch (Exception e)
                {
                    e.printStackTrace();
                }   
        }

        Thread.currentThread().suspend();
        
    }

    /**
     * Kills the thread. Stops listening!
     */
    public void killThread()
    {
        running = false;
    }
    /**
     * Add an event to the events ArrayList.
     * @param toAdd Event to add.
     */
    public void addEvent(IMouseEvent toAdd)
    {
        Events.add(toAdd);
    }
    /**
     * Removes an event from the events ArrayList.
     * @param toRemove Event to remove.
     */
    public void removeEvent(IMouseEvent toRemove)
    {
        Events.remove(toRemove);
    }
}
